package com.sfh.tourist.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.engine.ImageEngine;
import com.zhihu.matisse.internal.entity.CaptureStrategy;

import java.util.List;

public class ImageSelector {
    public static ImageSelector create() {
        return new ImageSelector();
    }


    boolean showCamera = true;
    int maxSelectable = 1;

    private ImageSelector() {
    }

    public ImageSelector showCamera(boolean show) {
        this.showCamera = show;
        return this;
    }

    public ImageSelector count(int count) {
        this.maxSelectable = count;
        return this;
    }

    public ImageSelector single() {
        return this;
    }

    public ImageSelector multi() {
        return this;
    }


    public void start(Activity activity, int requestCode) {
        MediaScannerConnection.scanFile(activity, new String[]{""}, null, null);
        Matisse.from(activity).choose(MimeType.of(MimeType.JPEG, MimeType.PNG))
                .countable(true)//设置从1开始的数字
                .thumbnailScale(0.85f) // 缩略图的比例
                .imageEngine(new ZPicassoEngine()) // 使用的图片加载引擎
                .captureStrategy(new CaptureStrategy(true,"com.sfh.tourist.base.WorkerFileProvider"))
                .capture(showCamera).maxSelectable(maxSelectable).forResult(requestCode);
    }

    public void start(Fragment fragment, int requestCode) {
        MediaScannerConnection.scanFile(fragment.getContext(), new String[]{""}, null, null);
        Matisse.from(fragment).choose(MimeType.of(MimeType.JPEG, MimeType.PNG))
                .countable(true)//设置从1开始的数字
                .thumbnailScale(0.85f) // 缩略图的比例
                .imageEngine(new ZPicassoEngine()) // 使用的图片加载引擎
                .capture(showCamera)
                .captureStrategy(new CaptureStrategy(true,"com.sfh.tourist.base.WorkerFileProvider"))
                .maxSelectable(maxSelectable).forResult(requestCode);
    }

    /***
     * 返回结果，数据集合可能为null
     * @param resultCode
     * @param data
     * @return
     */
    public static List<String> onActivityResult(int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            return  Matisse.obtainPathResult(data);
        }
        return null;

    }

    private class ZPicassoEngine implements ImageEngine {

        @Override
        public void loadThumbnail(Context context, int resize, Drawable placeholder, ImageView imageView, Uri uri) {
            Picasso.get().load(uri).placeholder(placeholder)
                    .resize(resize, resize)
                    .centerCrop()
                    .into(imageView);
        }

        @Override
        public void loadGifThumbnail(Context context, int resize, Drawable placeholder, ImageView imageView,
                                     Uri uri) {
            loadThumbnail(context, resize, placeholder, imageView, uri);
        }

        @Override
        public void loadImage(Context context, int resizeX, int resizeY, ImageView imageView, Uri uri) {
            Picasso.get().load(uri).resize(resizeX, resizeY).priority(Picasso.Priority.HIGH)
                    .centerInside().into(imageView);
        }

        @Override
        public void loadGifImage(Context context, int resizeX, int resizeY, ImageView imageView, Uri uri) {
            loadImage(context, resizeX, resizeY, imageView, uri);
        }

        @Override
        public boolean supportAnimatedGif() {
            return false;
        }
    }
}
